package com.xiaobai.service;

import com.alibaba.fastjson.JSON;
import com.xiaobai.dao.AchievementDao;
import com.xiaobai.pojo.Achievement;
import com.xiaobai.vo.AllAchievementItemAllList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class UnitAchievementService {

    @Autowired
    private AchievementDao achievementDao;

//    @Async("getAsyncExecutor")
    public void saveAchievement(String student_id, AllAchievementItemAllList allAchievementItemAllList){

        /**
         * 1.截取学号前两位
         * 2.根据前两位来写出八个学期18-> 1801 1802 .....  19->1901 1902
         */
        Integer gender=Integer.parseInt(student_id.substring(0,2));
        String[] genders={"20"+gender+"01","20"+gender+"02","20"+(gender+1)+"01","20"+(gender+1)+"02","20"+(gender+2)+"01","20"+(gender+2)+"02","20"+(gender+3)+"01","20"+(gender+3)+"02"};

        List<Achievement> list=new ArrayList<>();

        Achievement achievement=new Achievement();
        achievement.setStudentId(student_id);

        //存入公选课成绩
        achievement.setTerm("00");
        achievement.setContent(JSON.toJSONString(allAchievementItemAllList.getZeroAchievementList()));

        list.add(achievement);//放入公选课成绩

        //循环八次八个学期 (存入成绩)
        for(int i=1;i<=8;i++){

            Integer index=i-1;

            switch (i){
                case 1:
                    Achievement achievement1=new Achievement();
                    achievement1.setStudentId(student_id);
                    achievement1.setTerm(genders[index]);
                    achievement1.setContent(JSON.toJSONString(allAchievementItemAllList.getOneAchievementList()));
                    list.add(achievement1);
                    break;
                case 2:
                    Achievement achievement2=new Achievement();
                    achievement2.setStudentId(student_id);
                    achievement2.setTerm(genders[index]);
                    achievement2.setContent(JSON.toJSONString(allAchievementItemAllList.getTwoAchievementList()));
                    list.add(achievement2);
                    break;
                case 3:
                    Achievement achievement3=new Achievement();
                    achievement3.setStudentId(student_id);
                    achievement3.setTerm(genders[index]);
                    achievement3.setContent(JSON.toJSONString(allAchievementItemAllList.getThreeAchievementList()));
                    list.add(achievement3);
                    break;
                case 4:
                    Achievement achievement4=new Achievement();
                    achievement4.setStudentId(student_id);
                    achievement4.setTerm(genders[index]);
                    achievement4.setContent(JSON.toJSONString(allAchievementItemAllList.getFourAchievementList()));
                    list.add(achievement4);
                    break;
                case 5:
                    Achievement achievement5=new Achievement();
                    achievement5.setStudentId(student_id);
                    achievement5.setTerm(genders[index]);
                    achievement5.setContent(JSON.toJSONString(allAchievementItemAllList.getFiveAchievementList()));
                    list.add(achievement5);
                    break;
                case 6:
                    Achievement achievement6=new Achievement();
                    achievement6.setStudentId(student_id);
                    achievement6.setTerm(genders[index]);
                    achievement6.setContent(JSON.toJSONString(allAchievementItemAllList.getSixAchievementList()));
                    list.add(achievement6);
                    break;
                case 7:
                    Achievement achievement7=new Achievement();
                    achievement7.setStudentId(student_id);
                    achievement7.setTerm(genders[index]);
                    achievement7.setContent(JSON.toJSONString(allAchievementItemAllList.getSevenAchievementList()));
                    list.add(achievement7);
                    break;
                case 8:
                    Achievement achievement8=new Achievement();
                    achievement8.setStudentId(student_id);
                    achievement8.setTerm(genders[index]);
                    achievement8.setContent(JSON.toJSONString(allAchievementItemAllList.getEightAchievementList()));
                    list.add(achievement8);
                    break;
            }

        }


        //开始批量插入成绩
        achievementDao.piLiangeInsertAchievement(list);

    }

}
